package com.oil.petro.eshop.dao;

import com.oil.petro.eshop.entity.Category;
import java.util.Collections;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("CategoryDAO")
public class CategoryDAOImpl implements CategoryDAO {

    @Autowired
    private SessionFactory sessionfactory;

    @Override
    public Category get(int id) {
        return (Category) sessionfactory.getCurrentSession().get(Category.class, id);
    }

    @Override
    public void delete(Category category) {
        sessionfactory.getCurrentSession().delete(category);
    }

    @Override
    public int save(Category category) {
        return (int) sessionfactory.getCurrentSession().save(category);
    }

    @Override
    public void update(Category category) {
        sessionfactory.getCurrentSession().update(category);
    }

    @Override
    public List<Category> getList() {
        return sessionfactory.getCurrentSession().createCriteria(Category.class)
                .addOrder(Property.forName("name").asc())
                .list();
    }

//     @Override
//    public boolean isPersonByLogin(String login) {
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        session.beginTransaction();
//        Query q = session.createQuery("Select login from Account where login='" + login + "'");
//        String user = (String) q.uniqueResult();
//
//        System.out.println(user + "co tu jest");
//        session.getTransaction().commit();
//        session.close();
//        boolean result = false;
//        if (null != user) {
//            result = true;
//        }
//        logger.info("AccountDaoImpl zwraca obiekt account");
//        return result;
//    }
    public int getId(String name) {
        Query query = sessionfactory.getCurrentSession().createQuery("from Category WHERE name='" + name + "'");
        System.out.println("ssss " + query.getQueryString());
        Category category = (Category) query.uniqueResult();
        return category.getId();

    }

}
